package com.letv.android.client.album.controller;

import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;

import com.letv.android.young.client.R;
import com.letv.android.client.activity.AlbumPlayRoomActivity;
import com.letv.business.flow.album.AlbumPlayFlowObservable;
import com.letv.business.flow.album.AlbumPlayFlowObservable.VideoTitleNotify;

import java.util.Observable;
import java.util.Observer;

/**
 * 点播网络切换提示布局控制器
 * 
 * @author zhuqiao
 *
 */
public class AlbumRoomNetChangeController implements Observer {
    private AlbumPlayRoomActivity mActivity;

    private View mContainView;
    private View mInterputView;
    private View mContinueView;
    private View mClickView;

    public AlbumRoomNetChangeController(AlbumPlayRoomActivity activity) {
        mActivity = activity;
        init();
    }

    private void init() {
        mContainView = mActivity.findViewById(R.id.album_net_frame);
        mInterputView = mActivity.findViewById(R.id.net_top_interput_click);
        mContinueView = mActivity.findViewById(R.id.album_net_change_continue);
        mClickView = mActivity.findViewById(R.id.album_net_change_click_frame);

        mInterputView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // 拦截下层view事件
            }
        });
        mContinueView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mActivity.getFlow() != null) {
                    mActivity.getFlow().startPlayWith3g();
                }
            }
        });
    }

    /**
     * 显示3g提示
     * 
     * @param isBlack
     */
    private void show3gLayout(boolean isBlack) {
        mContainView.setVisibility(View.VISIBLE);

        if (isBlack) {
            mClickView.setVisibility(View.GONE);
        } else {
            mClickView.setVisibility(View.VISIBLE);
        }

        mActivity.getErrorTopController().show();
        mInterputView.setVisibility(View.VISIBLE);
    }

    /**
     * 隐藏3g提示
     */
    private void hide3gLayout() {
        mContainView.setVisibility(View.GONE);
        mActivity.getErrorTopController().hide();
    }

    @Override
    public void update(Observable observable, Object data) {
        if (data instanceof String) {
            String notify = (String) data;
            if (TextUtils.equals(AlbumPlayFlowObservable.ON_SHOW_3G, notify)) {
                // 显示网络切换layout
                show3gLayout(false);
            } else if (TextUtils.equals(AlbumPlayFlowObservable.ON_SHOW_3G_WITH_BLACK, notify)) {
                // 显示网络切换layout
                show3gLayout(true);
            } else if (TextUtils.equals(AlbumPlayFlowObservable.ON_HIDE_3G, notify)) {
                hide3gLayout();
            }
        } else if (data instanceof VideoTitleNotify) {
            // 设置标题
            String title = ((VideoTitleNotify) data).title;
            mActivity.getErrorTopController().setTitle(title);
        }
    }

}
